package com.yourbatis.syntax;

import com.yourbatis.interpreter.Environment;

public class ForStatement implements Statement{

    public ForStatement(Statement init, Expression condition, Expression update, Statement body) {
        this.init = init;
        this.condition = condition;
        this.update = update;
        this.body = body;
    }

    private Statement init;

    private Expression condition;

    private Expression update;

    private Statement body;

    @Override
    public Object execute() {
        Environment.enterScope();
        Object val = null;
        try {
            init.execute();
            while((Boolean) condition.eval()){
                val = body.execute();
                update.eval();
            }
        }finally {
            Environment.exitScope();
        }
        return val;
    }
}
